﻿///就绪函数
$(function () {
    show();
});

///工人工时弹框添加
layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;

    // 打开添加弹窗的按钮点击事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                title: "新增工人工时",
                content: `
                    <form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourNumbering" autocomplete="off" id="workerHourNumbering" placeholder="请输入工人编号" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人姓名</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourName" autocomplete="off" id="workerHourName" placeholder="请输入工人姓名" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">所属部门</label>
                    <div class="layui-col-md6">
                        <select name="workerHourDepartment" id="workerHourDepartment" >
                            <option value="">选择部门</option>
                            <option value="生产部">生产部</option>
                            <option value="销售部">销售部</option>
                            <option value="指挥部">指挥部</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourPhone" autocomplete="off" id="workerHourPhone" placeholder="请输入联系方式" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工作时长</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourDuration" autocomplete="off" id="workerHourDuration" placeholder="请输入工作时长" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">休息时长</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourRest" autocomplete="off" id="workerHourRest" placeholder="请输入休息时长" class="layui-input">
                    </div>
                    </div>

                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <button class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                     <button class="layui-btn" lay-submit lay-filter="demo-login">添加</button>
                    </div>
                    <form>`,
                success: function () {

                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {
                        var field = data.field;
                        console.log(field)
                        $.ajax({
                            url: '/WorkerHour/GetAddWorkerHour',
                            type: 'post',
                            data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                            success: function (res) {
                                var s = JSON.parse(res)
                                console.log(res)
                                if (s.data > 0) {
                                    layer.alert("添加成功");
                                    setTimeout(function () {
                                        location.reload();
                                    }, 2000);
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });

        }
    })
});
//工人工时显示
function show() {
    layui.use('table', function () {
        var table = layui.table;
        //查询
        var workerHourNumbering = $("#workerHourNumbering").val();
        var workerHourState = $("#workerHourState").val();
        $.ajax({
            url: "/WorkerHour/GetShowWorkerHour",
            type: "get",
            data: {
                workerHourNumbering: workerHourNumbering,
                workerHourState: workerHourState,
                pageIndex: 1,
                pageSize: 100,
                token: localStorage.getItem("token"),
            },
            success: function (res) {
                var res = JSON.parse(res);
                if (res.code == 0) {
                    console.log(res.data)
                    table.render({
                        elem: '#carriertable',//对应HTML中表格的id
                        data: res.data.plist,//绑定获取的数据
                        toolbar: '#toolbarDemo',//导出
                        defaultToolbar: ['exports', 'print'],//导出
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'workerHourNumbering', title: '工人编号',width: 120 },
                            { field: 'workerHourName', title: '工人姓名', width: 120 },
                            { field: 'workerHourPhone', title: '手机号码', width: 120 },
                            { field: 'workerHourDuration', title: '工作时长', width: 120 },
                            { field: 'workerHourDepartment', title: '所属部门', width: 120 },
                            { field: 'workerHourRest', title: '休息时长', width: 120 },
                            {
                                field: 'workerHourState', title: '状态', width: 120,
                                templet: function (d) {
                                    if (d.workerHourState==0) {
                                        return '<span style = "color:black;">上工</span>'
                                    }
                                    if (d.workerHourState == 1) {
                                        return '<span style = "color:green;">请假</span>'
                                    }
                                    if (d.workerHourState == 2) {
                                        return '<span style = "color:red;">旷工</span>'
                                    }
                                }
                            },

                            {
                                field: '', title: '操作', templet: function (d) {
                                    return '<a class="layui-btn layui-btn-edit layui-btn-xs" lay-on="test-page-edit" onclick="updateData(' + d.workerHourId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                        '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.workerHourId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>' +
                                        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="storagemodel(' + d.workerHourId + ')"><i class="layui-icon layui-icon-search"></i>查看</a>'
                                }
                            }
                        ]],//<button id="printButton" class="layui-btn layui-btn-normal">打印</button>
                        page: true, // 是否显示分页
                        count: res.data.listcount,
                        limits: [3, 4, 5],
                        limit: 3,// 每页默认显示的数量
                        curr: res.data.pagecount,
                    })
                }
                //else {
                //    console.log(res.msg);
                //}
            },
            error: function () {
                console.log("获取数据失败");
                // 可以考虑显示一个错误消息给用户
            }
        });


    })
}

//删除工人工时
window.deleteData = function (id) {
    console.log(id);
    layer.confirm('确定要删除该条数据吗？', function (index) {
        $.ajax({
            type: "Post",
            url: "/WorkerHour/GetDeleteWorkerHour",  // 替换为实际的后台删除数据接口地址
            data: { data: JSON.stringify({ workerHourId: id }), token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                if (res.data >= 1) {
                    layer.alert("删除成功");
                    show();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");

                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
        layer.close(index);
    });
};

//反填
var obj = {
    workerHourId: '',
    workerHourNumberings: '',
    workerHourName: '',
    workerHourPhone: '',
    workerHourDuration: '',
    workerHourDepartment: '',
    workerHourRest:'',
    addName: '',
    addTime: ''
}
//反填修改
window.updateData = function (id) {
    // 先根据id获取要编辑的数据，发起Ajax请求
    var form = layui.form;
    $.ajax({
        type: "get",
        url: "/WorkerHour/GetBackFillWorkerHour",  // 替换为实际的后台根据id获取数据的接口地址
        data: { workerHourId: id, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            console.log(res)
            $(res.data).each(function () {
                $('#workerHourNumberings').val(this.workerHourNumbering)
                $('#workerHourName').val(this.workerHourName)
                $('#workerHourPhone').val(this.workerHourPhone)
                $('#workerHourDuration').val(this.workerHourDuration)
                $('#workerHourDepartment').val(this.workerHourDepartment)
                $('#workerHourRest').val(this.workerHourRest)
                $('#addName').val(this.addName)
                $('#addTime').val(this.addTime)
                obj.workerHourId = this.workerHourId
                obj.workerHourNumberings = this.workerHourNumbering
                obj.workerHourName = this.workerHourName
                obj.workerHourPhone = this.workerHourPhone
                obj.workerHourDuration = this.workerHourDuration
                obj.workerHourDepartment = this.workerHourDepartment
                obj.workerHourRest = this.workerHourRest
                obj.addName = this.addName
                obj.addTime = this.addTime
                //渲染下拉框
                form.render('select');
            })

        },
    });
    layer.open({
        type: 1,
        title: '编辑工人工时',
        content: `<form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourNumberings" autocomplete="off" id="workerHourNumberings" placeholder="请输入工人编号" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人姓名</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourName" autocomplete="off" id="workerHourName" placeholder="请输入工人姓名" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">所属部门</label>
                    <div class="layui-col-md6">
                        <select name="workerHourDepartment" id="workerHourDepartment" >
                            <option value="">选择部门</option>
                            <option value="生产部">生产部</option>
                            <option value="销售部">销售部</option>
                            <option value="指挥部">指挥部</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourPhone" autocomplete="off" id="workerHourPhone" placeholder="请输入联系方式" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工作时长</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourDuration" autocomplete="off" id="workerHourDuration" placeholder="请输入工作时长" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">休息时长</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourRest" autocomplete="off" id="workerHourRest" placeholder="请输入休息时长" class="layui-input">
                    </div>
                    </div>

                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <div class="layui-form-item" style="text-align:center">
                    <button class="layui-btn" lay-submit lay-filter="demo-login">修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">取消</button>
              </div>
                    </div>
                    <form>
                    `,
        area: ['700px', '400px'],
        //修改
        success: function () {
            var obj1 = {
                workerHourId: '',
                workerHourNumberings: '',
                workerHourName: '',
                workerHourPhone: '',
                workerHourDuration: '',
                workerHourDepartment: '',
                workerHourRest: '',
                addName: '',
                addTime: ''
            };
            console.log(obj1)
            // 对弹层中的表单进行初始化渲染
            form.render();
            // 表单提交事件
            form.on('submit(demo-login)', function (data) {
                var field = data.field;
                obj1.workerHourId = obj.workerHourId
                obj1.workerHourNumbering = field.workerHourNumberings
                obj1.workerHourName = field.workerHourName
                obj1.workerHourPhone = field.workerHourPhone
                obj1.workerHourDuration = field.workerHourDuration
                obj1.workerHourDepartment = field.workerHourDepartment
                obj1.workerHourRest = field.workerHourRest
                obj1.addName = obj.addName
                obj1.addTime = obj.addTime
                $.ajax({
                    url: '/WorkerHour/GetUpdateWorkerHour',
                    type: 'post',
                    data: { obj1, token: localStorage.getItem("token") },
                    success: function (res) {
                        var res = JSON.parse(res)
                        console.log(field)
                        console.log(res)
                        if (res.data >= 1) {
                            layer.alert("修改成功");
                            setTimeout(function () {
                                location.reload();
                            }, 2000);
                        }
                        else {
                            layer.alert("修改失败");
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            });
        }
    })
}


//查看
var obj = {
    workerHourId: '',
    workerHourNumberings: '',
    workerHourName: '',
    workerHourPhone: '',
    workerHourDuration: '',
    workerHourDepartment: '',
    workerHourRest: '',
    addName: '',
    addTime: ''
}
//查看
window.storagemodel = function (id) {
    // 先根据id获取要编辑的数据，发起Ajax请求
    var form = layui.form;
    $.ajax({
        type: "get",
        url: "/WorkerHour/GetBackFillWorkerHour",  // 替换为实际的后台根据id获取数据的接口地址
        data: { workerHourId: id, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            console.log(res)
            $(res.data).each(function () {
                $('#workerHourNumberings').val(this.workerHourNumbering)
                $('#workerHourName').val(this.workerHourName)
                $('#workerHourPhone').val(this.workerHourPhone)
                $('#workerHourDuration').val(this.workerHourDuration)
                $('#workerHourDepartment').val(this.workerHourDepartment)
                $('#workerHourRest').val(this.workerHourRest)
                $('#addName').val(this.addName)
                $('#addTime').val(this.addTime)
                obj.workerHourId = this.workerHourId
                obj.workerHourNumberings = this.workerHourNumbering
                obj.workerHourName = this.workerHourName
                obj.workerHourPhone = this.workerHourPhone
                obj.workerHourDuration = this.workerHourDuration
                obj.workerHourDepartment = this.workerHourDepartment
                obj.workerHourRest = this.workerHourRest
                obj.addName = this.addName
                obj.addTime = this.addTime
                //渲染下拉框
                form.render('select');
            })

        },
    });
    layer.open({
        type: 1,
        title: '编辑工人工时',
        content: `<form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourNumberings" autocomplete="off" id="workerHourNumberings" placeholder="请输入工人编号" disabled class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人姓名</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourName" autocomplete="off" id="workerHourName" placeholder="请输入工人姓名" disabled class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">所属部门</label>
                    <div class="layui-col-md6">
                        <select name="workerHourDepartment" disabled id="workerHourDepartment" >
                            <option value="">选择部门</option>
                            <option value="生产部">生产部</option>
                            <option value="销售部">销售部</option>
                            <option value="指挥部">指挥部</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourPhone" autocomplete="off" id="workerHourPhone" placeholder="请输入联系方式" disabled class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工作时长</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourDuration" autocomplete="off" id="workerHourDuration" placeholder="请输入工作时长" disabled class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">休息时长</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerHourRest" autocomplete="off" id="workerHourRest" placeholder="请输入休息时长" disabled class="layui-input">
                    </div>
                    </div>

                    
                    <form>
                    `,
        area: ['700px', '400px'],
    })
}

        